package com.songshuang.myutilsboot.stlf.core;

import com.songshuang.myutilsboot.stlf.io.Constants;
import com.songshuang.myutilsboot.stlf.model.Models;

import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public final class ReportWriter {
    private ReportWriter() {}

    public static void writeResAndDat(String caseStr, Models.ForecastInfo fi) {
        try {
            File dir = new File(Constants.FORCAST_RES_PATH);
            if (!dir.exists()) dir.mkdirs();
            File res = new File(dir, caseStr + ".res");
            try (FileWriter fw = new FileWriter(res, false)) {
                fw.write("Start_time=" + fi.start_time + "\n");
                fw.write("End_time  =" + fi.end_time + "\n");
                fw.write("----------------------------------------\n");
                // His Report
                fw.write("His Report\n");
                fw.write("----------------------------------------\n");
                fw.write("\n Date |Sd |MW0 |MW  |Err |Pct\n");
                for (Models.SigSdInf inf : fi.His_Report) {
                    fw.write(String.format(" %s %4d %f %f %f %f\n", inf.Date, inf.Sd, inf.MW0, inf.MW, inf.Err, inf.Pct));
                }
                fw.write("----------------------------------------\n");
                // Fore Report
                fw.write("Fore Report\n");
                fw.write("----------------------------------------\n");
                fw.write("\n Date |Sd |MW0 |MW\n");
                for (Models.SigSdInf inf : fi.Fore_Report) {
                    fw.write(String.format(" %s %4d %f %f\n", inf.Date, inf.Sd, 0.0, inf.MW));
                }
                fw.write("----------------------------------------\n");
            }
            // .dat 与 .res 内容对齐（可根据需要扩展）
            File dat = new File(dir, caseStr + ".dat");
            try (FileWriter fw = new FileWriter(dat, false)) {
                String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                fw.write("Start_time=" + (fi.start_time != null ? fi.start_time : now) + "\n");
                fw.write("End_time  =" + (fi.end_time != null ? fi.end_time : now) + "\n");
                fw.write("----------------------------------------\n");
                fw.write("His Report\n");
                fw.write("----------------------------------------\n");
                fw.write("\n Date |Sd |MW0 |MW  |Err |Pct\n");
                for (Models.SigSdInf inf : fi.His_Report) {
                    fw.write(String.format(" %s %4d %f %f %f %f\n", inf.Date, inf.Sd, inf.MW0, inf.MW, inf.Err, inf.Pct));
                }
                fw.write("----------------------------------------\n");
                fw.write("Fore Report\n");
                fw.write("----------------------------------------\n");
                fw.write("\n Date |Sd |MW0 |MW\n");
                for (Models.SigSdInf inf : fi.Fore_Report) {
                    fw.write(String.format(" %s %4d %f %f\n", inf.Date, inf.Sd, 0.0, inf.MW));
                }
                fw.write("----------------------------------------\n");
            }
        } catch (Exception ignored) {}
    }
}


